package zhixin.generate.baidumapts;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @program: java-control-browser
 * @create: 2022-05-05 12:45
 **/
public class Main {
    public static void main(String[] args) throws IOException {
        System.setOut(new PrintStream(new FileOutputStream("输出.txt")));
        String html = FileUtils.readFileToString(new File("需要解析的html.html"), StandardCharsets.UTF_8);
        Document parse = Jsoup.parse(html);
        System.out.println("/**" + parse.title() + "**/");
        Element classContent = parse.getElementById("classContent");
        if (classContent == null) {
            return;
        }
        int a = 0;
        int b = 0;
        System.out.println("export declare namespace BMap {");
        while (true) {
            Element h2 = classContent.getElementById("a" + a);
            if (h2 == null) {
                break;
            }
            System.out.println("/**" + h2.text() + "**/");
            while (true) {
                Element class_item = classContent.getElementById("a" + a + "b" + b);
                if (class_item == null) {
                    break;
                }
                Element class_title = class_item.getElementsByClass("class_title").first();
                if (class_title != null) {
                    Element class_desc = class_item.getElementsByClass("class_desc").first();
                    if (class_desc != null) {
                        System.out.println("/**" + class_desc.text() + "**/");
                    }
                    String text = class_title.text();
                    System.out.println("    interface " + text + " {");
                    Element class_constr = class_item.getElementsByClass("class_constr").first();
                    if (class_constr != null) { // 构造器
                        Elements class_constr_content = class_constr.getElementsByClass("class_constr_content");
                        for (Element element : class_constr_content) {
                            Element td2 = element.getElementsByTag("td").get(1);
                            System.out.println("        /**\n" +
                                    "         * " + td2.text() + "\n" +
                                    "         */");
                            Element td1 = element.getElementsByTag("td").get(0);
                            String constructor = td1.text().replaceFirst(text, "constructor").replaceAll(":", "?:");
                            System.out.println(constructor + ": void");
                        }
                    }
                    Element class_method = class_item.getElementsByClass("class_method").first();
                    if (class_method != null) { // 方法
                        Elements class_method_content = class_method.getElementsByClass("class_method_content");
                        for (Element element : class_method_content) {
                            Elements td = element.getElementsByTag("td");
                            Element td1 = td.get(0);
                            Element td2 = td.get(1);
                            Element td3 = td.get(2);
                            String returnText = td2.text();
                            returnText = returnText.replaceAll("Null","null");
                            if (Arrays.asList(new String[]{"None","none",""}).contains(returnText)) {
                                returnText = "void";
                            }
                            System.out.println("" +
                                    "        /**\n" +
                                    "         * " + td3.text() + "\n" +
                                    "         */\n" +
                                    "        " + td1.text().replaceAll("Null","null") + ": " + returnText + "");
                        }
                    }
                    Element class_eve = class_item.getElementsByClass("class_eve").first();
                    if (class_eve != null) { // 事件
                        Elements class_eve_content = class_eve.getElementsByClass("class_eve_content");
                        for (Element element : class_eve_content) {
                            Elements td = element.getElementsByTag("td");
                            Element td1 = td.get(0);
                            Element td2 = td.get(1);
                            Element td3 = td.get(2);
                            String canshu = td2.text();
                            canshu = canshu.replaceFirst("event","");
                            canshu+=": any";
                            System.out.println("/**\n" +
                                    "         * " + td3.text() + "\n" +
                                    "         */\n" +
                                    "        " + td1.text() + ": (" + canshu + ") => void");
                        }
                    }
                    Element class_proper = class_item.getElementsByClass("class_proper").first();
                    if(class_proper!=null){ // 字面量
                        Elements class_proper_content = class_proper.getElementsByClass("class_proper_content");
                        for (Element element : class_proper_content) {
                            Elements td = element.getElementsByTag("td");
                            Element td1 = td.get(0);
                            Element td2 = td.get(1);
                            Element td3 = td.get(2);
                            String type = td2.text();
                            if("Array".equals(type)) {
                                type+="<any>";
                            }
                            System.out.println("        /**\n" +
                                    "         * "+td3.text()+"\n" +
                                    "         */\n" +
                                    "        "+td1.text()+"?: "+ type +"");
                        }
                    }
                    Element class_constant = class_item.getElementsByClass("class_constant").first();
                    if(class_constant!=null){ // 常量
                        Elements class_constant_content = class_constant.getElementsByClass("class_constant_content");
                        for (Element element : class_constant_content) {
                            Elements td = element.getElementsByTag("td");
                            Element td1 = td.get(0);
                            Element td2 = td.get(1);
                            System.out.println("        /**\n" +
                                    "         * "+td2.text()+"\n" +
                                    "         */\n" +
                                    "        "+td1.text()+"?: any");
                        }
                    }
                    System.out.println("}");
                }
                b++;
            }
            a++;
            b=0;
        }
        System.out.println("}");
    }
}
